package com.my.launcher.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.StringUtils;


/**
 * 启动监听
 * @author timo 2021/2/6
 */
@Configuration
public class StartEventListener {
    private static final Logger log;

    @Async
    @Order
    @EventListener({WebServerInitializedEvent.class})
    public void afterStart(final WebServerInitializedEvent event) {
        final Environment environment = event.getApplicationContext().getEnvironment();
        final String appName = environment.getProperty("spring.application.name").toUpperCase();
        final int localPort = event.getWebServer().getPort();
        final String profile = StringUtils.arrayToCommaDelimitedString((Object[]) environment.getActiveProfiles());
//        ---[TEST]---启动完成，当前使用的端口:[9527]，环境变量:[@env@]---
        StartEventListener.log.info("---[{}]---\u542f\u52a8\u5b8c\u6210\uff0c\u5f53\u524d\u4f7f\u7528\u7684\u7aef\u53e3:[{}]\uff0c\u73af\u5883\u53d8\u91cf:[{}]---", new Object[]{appName, localPort, profile});
    }

    static {
        log = LoggerFactory.getLogger((Class) StartEventListener.class);
    }
}
